package by.qizer.supercamp.dao;

import by.qizer.supercamp.domain.Auto;
import by.qizer.supercamp.domain.Guard;
import by.qizer.supercamp.domain.Inside;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Pribytkoyski Alexey
 *         User: qizer
 *         Date: 08.04.12
 */
@Repository
public class InsideDAOImpl implements InsideDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void addInside(Inside inside) {
        Auto auto = findAuto(inside.getAutoId().getRegNumber());
        Guard guard = findGuard(inside.getGuardId().getLastName());

        if ((auto == null) || (guard == null)) {
//            print("auto or guard = null");
            return;
        }

        if (findAutoInInside(auto.getId())) {
            return;
        }

        inside.setAutoId(auto);
        inside.setGuardId(guard);
//        print("successfully set inside and goto save");

        sessionFactory.getCurrentSession().save(inside);
    }

    @SuppressWarnings("unchecked")
    public List<Inside> listInside() {
        return sessionFactory.getCurrentSession().createQuery("from Inside").list();
    }

    public Inside removeInside(Integer id) {
        Inside inside = (Inside) sessionFactory.getCurrentSession().load(Inside.class, id);
        if (null != inside) {
            sessionFactory.getCurrentSession().delete(inside);
        }
//        print(inside.getAutoId().getId().toString());
//        print(inside.getGuardId().getLastName().toString());
//        print(inside.getInTime().toString());
        print("\n\n\t delete from db");
        return inside;
    }


    private Auto findAuto(String regNumber) {
        String hql = "from Auto where regNumber='" + regNumber + "'";
        List<Auto> answer = sessionFactory.getCurrentSession().createQuery(hql).list();

        if (answer.isEmpty()) {
//            print("find auto - Error");
            return null;
        } else {
//            print("find auto successfully");
            return answer.get(0);
        }
    }

    private Guard findGuard(String lastName) {
        String hql = "from Guard where lastName='" + lastName + "'";
        List<Guard> answer = sessionFactory.getCurrentSession().createQuery(hql).list();

        if (answer.isEmpty()) {
//            print("find guard - Error!");
            return null;
        } else {
//            print("find guard successfully");
            return answer.get(0);
        }
    }

    private Boolean findAutoInInside(Integer id) {
        String hql = "from Inside where autoId.id=" + id;
        List<Auto> answer = sessionFactory.getCurrentSession().createQuery(hql).list();

        if (answer.isEmpty()) {
//            print("auto absent in inside and id = " + id);
            return false;
        } else {
//            print("auto present in inside and id = " + id);
            return true;
        }
    }

    private void print(String str) {
        System.out.println();
        System.out.println("                       " + str);
        System.out.println();
    }
}
